package com.gxc.datacore.query.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.gxc.datacore.metadata.domain.Column;
import com.gxc.datacore.query.cache.DictionaryCache;
import com.gxc.datacore.query.service.DataTranslateService;
import com.gxc.dbcore.util.BeanMap;
import com.gxc.webcore.util.StringUtil;

@Service
public class DataTranslateServiceImpl implements DataTranslateService {

	public List<BeanMap> translate(List<Column> columnList,
			List<BeanMap> dataList) {
		for (Column column : (List<Column>) columnList) {
			dataList = translate(column, dataList);
		}
		return dataList;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<BeanMap> translate(Column column, List<BeanMap> dataList) {
		if (!StringUtil.isTrimEmpty(column.getDictionary_id())) {
			Map dicmap = DictionaryCache.get(column.getDictionary_id());
			if (dicmap != null) {
				for (Map datamap : dataList) {
					if (datamap.get(column.getColumn_name()) != null) {
						if (dicmap.get(datamap.get(column.getColumn_name())) != null) {
							datamap.put(column.getColumn_name(), dicmap
									.get(datamap.get(column.getColumn_name())));
						}
					}
				}
			}
		}
		return dataList;
	}

	public BeanMap translate(List<Column> columnList, BeanMap map) {
		for (Column column : (List<Column>) columnList) {
			map = translate(column, map);
		}
		return map;
	}

	@SuppressWarnings("rawtypes")
	public BeanMap translate(Column column, BeanMap map) {
		if (!StringUtil.isTrimEmpty(column.getDictionary_id())) {
			if (map.get(column.getColumn_name()) != null) {
				Map dicmap = DictionaryCache.get(column.getDictionary_id());
				if (dicmap != null) {
					if (dicmap.get(map.get(column.getColumn_name())) != null) {
						map.put(column.getColumn_name(),
								dicmap.get(map.get(column.getColumn_name())));
					}
				}
			}
		}
		return map;
	}

}
